package top.mypath.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import org.apache.commons.lang.StringUtils;
import top.mypath.model.OrderDetail;
import top.mypath.model.vo.InvoiceCheckVO;
import top.mypath.model.vo.OrderVO;
import top.mypath.model.vo.SettlementVO;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author LiYalin
 */
public class OrderDetailService {

    /**
     * 获取文件详情原始数据
     * @param filePath 原始文件路径
     * @return
     */
    public List<OrderDetail> getOriginalData(String filePath){
        OrderDetailExcelListener orderDetailExcelListener = new OrderDetailExcelListener();
        // 这里默认读取第一个sheet
        EasyExcel.read(filePath, OrderDetail.class, orderDetailExcelListener).sheet().doRead();
        List<OrderDetail> cachedDataList = orderDetailExcelListener.getCachedDataList();
        return cachedDataList.stream().peek(orderDetail -> {
            if (StringUtils.isNotBlank(orderDetail.getCommodityName())) {
                String foreignNames = orderDetail.getForeignNames();
                String[] array = foreignNames.split("\\s+", 3);
                if (array.length >= 1) {
                    orderDetail.setBrand(array[0]);
                }
                if (array.length >= 2) {
                    orderDetail.setSplitedCommodityName(array[1]);
                }
                if (array.length == 3) {
                    orderDetail.setSpecification(array[2]);
                }
            }
        }).collect(Collectors.toList());
    }

    /**
     * 生成结算表数据
     * @param orderDetails 订单详情
     * @param orderVOS 订单信息集合
     * @return
     */
    public List<SettlementVO> generateSettlementData(List<OrderDetail> orderDetails, List<OrderVO> orderVOS,BigDecimal taxRate){
        Map<String, OrderVO> orderMap = orderVOS.stream().collect(Collectors.toMap(OrderVO::getBatchNumber, Function.identity()));
        List<SettlementVO> settlementVOS = orderDetails.stream().map(orderDetail -> {
            SettlementVO settlementVO = new SettlementVO();
            settlementVO.setBatchNumber(orderDetail.getBatchNumber());
            settlementVO.setCommodityName(orderDetail.getCommodityName());
            settlementVO.setSpecification(orderDetail.getSpecification());
            settlementVO.setPurchaseNum(orderDetail.getPurchaseNum());
            settlementVO.setUnit(orderDetail.getUnit());
            OrderVO orderVO = orderMap.get(orderDetail.getBatchNumber());
            if (ObjectUtil.isNotEmpty(orderVO)) {
                settlementVO.setInvoiceSerialNumber(orderVO.getInvoiceSerialNumber());
            }
            settlementVO.setPurchasePrice(orderDetail.getPurchasePrice());
            BigDecimal subtotal = orderDetail.getPurchasePrice().multiply(BigDecimal.valueOf(orderDetail.getPurchaseNum()));
            settlementVO.setSubtotal(subtotal);
            BigDecimal taxSubtotal = subtotal.multiply(taxRate);
            settlementVO.setTaxSubtotal(taxSubtotal);
            settlementVO.setTaxExcludedSubtotal(subtotal.subtract(taxSubtotal));
            return settlementVO;
        }).collect(Collectors.toList());
        return settlementVOS;
    }


    /**
     * 生成发票核对表
     * @param settlementVOS 结算表数据
     * @return
     */
    public List<InvoiceCheckVO> generateInvoiceCheck(List<SettlementVO> settlementVOS){
        ArrayList<InvoiceCheckVO> invoiceCheckVOS = new ArrayList<>();
        Map<String, List<SettlementVO>> map = settlementVOS.stream().collect(Collectors.groupingBy(SettlementVO::getInvoiceSerialNumber));
        for(Map.Entry<String,List<SettlementVO>> entry:map.entrySet()){
            List<SettlementVO> value = entry.getValue();
            InvoiceCheckVO invoiceCheckVO = new InvoiceCheckVO();
            invoiceCheckVO.setInvoiceSerialNumber(entry.getKey());
            invoiceCheckVO.setLineNum(value.size());
            value.forEach(v->{
                if(ObjectUtil.isNull(invoiceCheckVO.getTaxExcludedTotal())){
                    invoiceCheckVO.setTaxExcludedTotal(v.getTaxExcludedSubtotal());
                }else{
                    invoiceCheckVO.setTaxExcludedTotal(invoiceCheckVO.getTaxExcludedTotal().add(v.getTaxExcludedSubtotal()));
                }
                if(ObjectUtil.isNull(invoiceCheckVO.getTaxTotal())){
                    invoiceCheckVO.setTaxTotal(v.getTaxSubtotal());
                }else{
                    invoiceCheckVO.setTaxTotal(invoiceCheckVO.getTaxTotal().add(v.getTaxSubtotal()));
                }

            });
            invoiceCheckVO.setTotal(invoiceCheckVO.getTaxExcludedTotal().add(invoiceCheckVO.getTaxTotal()));
            invoiceCheckVOS.add(invoiceCheckVO);
        }
        return invoiceCheckVOS;
    }
}
